package com.powernode.controller;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.dao.ProdEsDao;
import com.powernode.model.ProdEs;
import com.powernode.service.SearchService;
import io.micrometer.core.instrument.search.Search;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 内容:
 * 作者: cy
 */
@RestController
@Api(tags = "搜索管理")
public class SearchController {


    @Autowired
    private SearchService searchService;

    @Autowired
    private ProdEsDao prodEsDao;

    @GetMapping("prod/prodListByTagId")
    @ApiOperation("根据标签id检索商品")
    public ResponseEntity<Page<ProdEs>> searchProdByTagId(Long tagId,
                                                          Integer size,
                                                          @RequestParam(required = false, defaultValue = "1") Integer current) {
        Page<ProdEs> prodEsPage = searchService.searchProdEsByTagId(tagId, size, current);
        return ResponseEntity.ok(prodEsPage);
    }


    @GetMapping("prod/pageProd")
    @ApiOperation("根据分类id检索商品")
    public ResponseEntity<Page<ProdEs>> searchProdByCategoryId(Long cateGoryId,
                                                               @RequestParam(required = false, defaultValue = "10") Integer size,
                                                               @RequestParam(required = false, defaultValue = "1") Integer current) {
        Page<ProdEs> prodEsPage = searchService.searchProdByCategoryId(cateGoryId, size, current);
        return ResponseEntity.ok(prodEsPage);
    }


    @GetMapping("search/searchProdPage")
    @ApiOperation("根据关键字检索商品")
    public ResponseEntity<Page<ProdEs>> searchProdByKeywords(String prodName,
                                                             @RequestParam(required = false, defaultValue = "10") Integer size,
                                                             @RequestParam(required = false, defaultValue = "1") Integer current,
                                                             Integer sort) {
        Page<ProdEs> prodEsPage = searchService.searchProdByKeywords(prodName, size, current, sort);
        return ResponseEntity.ok(prodEsPage);
    }
    @GetMapping("getProdEsByProdId")
    @ApiOperation("根据商品的ids检索商品集合")
    List<ProdEs> getProdEsByProdId(@RequestParam List<Long> prodIds) {
        Iterable<ProdEs> esIterable = prodEsDao.findAllById(prodIds);
        ArrayList<ProdEs> prodEsArrayList = new ArrayList<>();
        esIterable.forEach(prodEsArrayList::add);
        return prodEsArrayList;

    }


}
